#https://www.luogu.com.cn/problem/P1111
#一个最小生成树题
N, M = readline.split(' ').map(&:to_i)

class UnionSet
	attr :reserved_bangs

	def initialize(from,to_included)
		@uwp={}
		@reserved_bangs =to_included -from +1
		(from..to_included).each {|i| @uwp[i]=i}
	end

	def call(n)
		sup =@uwp[n]
		if(@uwp[sup]!=sup)
			@uwp[n] = call(sup)
		end
		return sup
	end
	
	def connect(a,b)
		@uwp[call(a)]=call(b)
		@reserved_bangs -=1
	end
end

Edges =(1..M).collect{x,y,t =readline.split(' ').map(&:to_i); [x,y,t] }.sort_by{|x,y,t|t}

$us =UnionSet.new(1,N)

$time=0
Edges.each do |x,y,t|
	if $us.call(x)!=$us.call(y)
		$us.connect(x,y)
		$time =$time <t ? t : $time
		if $us.reserved_bangs ==1 
			break
		end
	end
end

puts $us.reserved_bangs ==1 ? $time : -1



